﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CoreEx.Common.Data;
using CoreEx.Common.Extensions;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
namespace CoreEx.MicroMapper.Implementation
{
    [Implements(typeof(IMicroMapper<IDataReader, ITable>))]
    public class DataReaderToTableMapper : MicroMapper<IDataReader, ITable>
    {
        public override void Initialize(IServiceContainer source)
        {
            base.Initialize(source);
            TargetFactory =
                (dataReader) => source.GetService<IMicroMapper<IDataReader, ITable>>("DataReaderToTableFactoryMapper").Apply(dataReader);

            SourceAccessorSelector = (reader) => new List<IAccessorInfo>();
            TargetAccessorSelector = (reader) => new List<IAccessorInfo>();
            

            Explicit(
                (dataReader, table) =>
                {
                    var rowMapper = source.GetService<IMicroMapper<IDataRecord, IRow>>(table);
                    var rows = rowMapper.Apply(((IEnumerable)dataReader).Cast<IDataRecord>());
                    rows.ForEach(r => table.Rows.Add(r));
                });
        }
    }
}
